#include "eventwin.h"
#include "ui_eventwin.h"
#include <QDebug>
#include <QCursor>  //光标
#include <QPainter>

EventWin::EventWin(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::EventWin)
{
    ui->setupUi(this);
    resize(800,800);
    custButton = new CustomButton(this);


    //    this->setMouseTracking(true);
}

EventWin::~EventWin()
{
    delete ui;
}

void EventWin::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::RightButton)
        close();
}

void EventWin::mouseMoveEvent(QMouseEvent *event)
{
    //    qDebug()<<"globalPos:"<<event->globalPos();
    //    qDebug()<<"localPos:"<<event->localPos();
    //    qDebug()<<"Pos:"<<event->pos();
    //    qDebug()<<"screen:"<<event->screenPos();
    //    qDebug()<<"window:"<<event->windowPos();
    //    qDebug()<<"cursor:"<<QCursor::pos();
}

void EventWin::keyPressEvent(QKeyEvent *event)
{
    if(event->key() == Qt::Key_Escape)
        close();
}

void EventWin::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QPen pen;
    pen.setColor(QColor("#005179")); //品红
    pen.setWidth(6);
    //    pen.setStyle(Qt::DashLine);

    QBrush brush;
    brush.setColor(Qt::red);
    brush.setStyle(Qt::Dense1Pattern);
    painter.setBrush(brush);

    painter.setPen(pen);
    //平移坐标
    painter.translate(400,400);
    //抗锯齿
    painter.setRenderHint(QPainter::Antialiasing);
    if(flag == 1)
    {
        painter.drawEllipse(QPoint(0,0),100,100);
    }else{
        painter.drawRect(0,0,100,100);
    }
#if 0
    pen.setColor(Qt::green);
    painter.setPen(pen);
    painter.drawLine(QPoint(0,100),QPoint(0,90));

    painter.rotate(45);
    painter.drawLine(QPoint(0,100),QPoint(0,90));

    //绘制一条直线
    /*
    static const QPointF points[3] = {
        QPointF(10.0, 80.0),
        QPointF(20.0, 10.0),
        QPointF(80.0, 30.0)
    };
    painter.drawConvexPolygon(points, 3);
    */

#endif
}

void EventWin::timerEvent(QTimerEvent *e)
{
    static int num1 = 0;
    static int num2 = 0;
    if(timeId100 == e->timerId())
    {
        ui->label100->setText(QString::number(++num1));
    }
    else if(timeId1000 == e->timerId())
    {
        ui->label1000->setText(QString::number(++num2));
    }
}

void EventWin::on_cricleButton_clicked()
{
    //    flag = 1;
    //    update();
    timeId100 = startTimer(100);
    timeId1000 = startTimer(1000);
}

void EventWin::on_squreButton_clicked()
{
    killTimer(timeId100);
    killTimer(timeId1000);
    flag = 0;
    update();
}
